class ReportStatistic < ActiveRecord::Base
  belongs_to :report
  
  def self.get_salary_upper(salary)
    n_salary=ReportStatistic.log_amount(salary)
    int_salary=ReportStatistic.sma_amount(salary,n_salary).to_i+1
    return ReportStatistic.lag_amount(int_salary,n_salary)
  end
  
  def self.get_salary_lower(salary)
    n_salary=ReportStatistic.log_amount(salary)
    int_salary=ReportStatistic.sma_amount(salary,n_salary).to_i-1
    return ReportStatistic.lag_amount(int_salary,n_salary)
  end
  
  def self.get_salary_approx(salary)
    n_salary=ReportStatistic.log_amount(salary)  
    int_salary=ReportStatistic.sma_amount(salary,n_salary).round
    return ReportStatistic.lag_amount(int_salary,n_salary)
  end
  
private
  def self.log_amount(amount)
    return Math.log10(amount).to_i
  end
  
  def self.sma_amount(amount,n)
    if n>=0
      n.times {amount/=10.0}
      return amount
    else
      return 1
    end
  end
  
  def self.lag_amount(amount,n)
    if n>=0
      n.times {amount*=10}
      return amount
    else
      return 1
    end
  end
  
end

class ReportEducation < ReportStatistic
  belongs_to :education, :foreign_key=>"upper"
  
  def self.create_from_incumbent(incumbent)
    report_education=ReportStatistic::ReportEducation.new
    report_education.education=incumbent.report_education
    report_education.lower=1
    report_education.value=ReportSalary.get_salary_approx(incumbent.salary)
    return report_education
  end
end

class ReportOversea < ReportStatistic
  belongs_to :oversea, :foreign_key=>"upper"

  def self.create_from_incumbent(incumbent)
    report_oversea=ReportStatistic::ReportOversea.new
    report_oversea.oversea=incumbent.report_oversea
    report_oversea.lower=1
    report_oversea.value=ReportSalary.get_salary_approx(incumbent.salary)
    return report_oversea
  end
  
end

class ReportSalary < ReportStatistic

  def self.create_from_incumbent(incumbent)
    report_salary = ReportStatistic::ReportSalary.new
    report_salary.upper=ReportSalary.get_salary_upper(incumbent.salary)
    report_salary.lower=ReportSalary.get_salary_lower(incumbent.salary)
    report_salary.value=1
    return report_salary
  end
end

class ReportExp < ReportStatistic
  belongs_to :expstatistic, :class_name=>"StatisticCategory::ExpStatistic",:foreign_key=>"upper"
  
  def self.create_from_incumbent(incumbent)
    report_exp=ReportStatistic::ReportExp.new
    report_exp.expstatistic=StatisticCategory::ExpStatistic.get_category(incumbent.report_experience)
    report_exp.lower=1
    report_exp.value=ReportSalary.get_salary_approx(incumbent.salary)
    return report_exp
  end
  
end

class ReportAge < ReportStatistic
  belongs_to :agestatistic, :class_name=>"StatisticCategory::AgeStatistic",:foreign_key=>"upper"
  
  def self.create_from_incumbent(incumbent)
    report_age=ReportStatistic::ReportAge.new
    report_age.agestatistic=StatisticCategory::AgeStatistic.get_category(incumbent.report_age)
    report_age.lower=1
    report_age.value=ReportSalary.get_salary_approx(incumbent.salary)
    return report_age
  end
end

class ReportSex < ReportStatistic
  belongs_to :sexstatistic, :class_name=>"StatisticCategory::SexStatistic",:foreign_key=>"upper"
  
  def self.create_from_incumbent(incumbent)
    report_sex=ReportStatistic::ReportSex.new
    report_sex.sexstatistic=StatisticCategory::SexStatistic.get_category(incumbent.report_sex)
    report_sex.lower=1
    report_sex.value=ReportSalary.get_salary_approx(incumbent.salary)
    return report_sex
  end
end 
